﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vkUpploaderLib;
using vkUpploaderLib.Exceptions;

namespace vkUpploaderApp
{
    public sealed partial class frmLogin : Form
    {
        private bool _closePromt;

        public frmLogin()
        {
            InitializeComponent();
            this.Text = string.Format("{0}", StringResources.ProductName);

            this.lb_loginPromt.Text = StringResources.LoginPrompt;
            this.bt_login.Text = StringResources.LoginButton;
            AuthHelper.LoginError += AuthHelper_LoginError;
        }

        private void frmAuth_Shown(object sender, EventArgs e)
        {
     
        }

        private void bt_login_Click(object sender, EventArgs e)
        {
            if (AuthHelper.Auth())
            {
                Properties.Settings.Default.Token = AuthHelper.Token;
                Properties.Settings.Default.TokenExpires = AuthHelper.TokenExpires;
                Properties.Settings.Default.UserID = AuthHelper.UserID;
                Properties.Settings.Default.Save();

                _closePromt = false;
                Close();
            }
        }

        void AuthHelper_LoginError(object sender, ErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, StringResources.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }

        private void OnClosing(object sender, FormClosingEventArgs e)
        {
            if (!_closePromt)
            {
                e.Cancel = false;
                return;
            }

            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (MessageBox.Show(StringResources.ExitPromt, StringResources.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    e.Cancel = true;
                }

                Program.Terminate = true;
            }
        }
    }
}
